package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.FileConverterUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

@Service
public class RoadmapCmServiceImpl implements RoadmapCmService {

    @Autowired
    private RoadmapParamDao roadmapParamDao;

    @Autowired
    private RoadmapCmDao roadmapCmDao;

    @Autowired
    private RoadmapBigTypeDao roadmapBigTypeDao;

    @Autowired
    private RoadmapSmallTypeService roadmapSmallTypeService;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Value("${personal.config.file.file-save-url}")
    private String fileSaveUrl;

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private RoadmapCmStatusDao roadmapCmStatusDao;

    @Autowired
    private RoadmapH3cDao roadmapH3cDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "Roadmap CM 厂";

    @Transactional(transactionManager = "MysqlTransactionManager1")
    @Override
    public Result updateRoadmapCmByBigTypeAndCompany(String data) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            String roadmapBigTypeId = jsonObject.getString("roadmapBigTypeId");
            String pcbaCompanyId = jsonObject.getString("pcbaCompanyId");
            String versionY = jsonObject.getString("versionY");
            String versionN = jsonObject.getString("versionN");
            JSONArray roadmapParamArr = jsonObject.getJSONArray("roadmapParamArr");
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId)) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId)) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (versionY == null || "".equals(versionY)) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }

            String userId = UserHeaderHolder.getUserId();
            Date d = new Date();

            // 删除原先的
            List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, Integer.parseInt(roadmapBigTypeId), null);
            for (RoadmapParam roadmapParam : allRoadmapParam) {
                roadmapCmDao.deleteRoadmapCmByKeyAndCompany(roadmapParam.getRoadmapParamKey(), Integer.parseInt(pcbaCompanyId), Integer.parseInt(versionY), versionN);
            }

            // 操作日志
            operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, d, JSONObject.toJSONString(allRoadmapParam), null));

            List<RoadmapCm> list = new ArrayList<>();
            // 再新增
            for (Object o : roadmapParamArr) {
                JSONObject roadmapParamObj = (JSONObject) o;
                RoadmapCm addRoadmapCm = new RoadmapCm();
                addRoadmapCm.setRoadmapCmParamKey(roadmapParamObj.getString("roadmapCmParamKey"));
                addRoadmapCm.setRoadmapCmParamValue1(roadmapParamObj.getString("roadmapCmParamValue1"));
                addRoadmapCm.setRoadmapCmParamValue2(roadmapParamObj.getString("roadmapCmParamValue2"));
                addRoadmapCm.setRoadmapCmParamValue3(roadmapParamObj.getString("roadmapCmParamValue3"));
                addRoadmapCm.setSort(Integer.parseInt(roadmapParamObj.getString("sort")));
                addRoadmapCm.setStatus(Integer.parseInt(roadmapParamObj.getString("status")));
                addRoadmapCm.setAbilityMismatchCauses1(roadmapParamObj.getString("abilityMismatchCauses1"));
                addRoadmapCm.setAbilityImprovePlan1(roadmapParamObj.getString("abilityImprovePlan1"));
                addRoadmapCm.setAbilityMismatchCauses2(roadmapParamObj.getString("abilityMismatchCauses2"));
                addRoadmapCm.setAbilityImprovePlan2(roadmapParamObj.getString("abilityImprovePlan2"));
                addRoadmapCm.setAbilityMismatchCauses3(roadmapParamObj.getString("abilityMismatchCauses3"));
                addRoadmapCm.setAbilityImprovePlan3(roadmapParamObj.getString("abilityImprovePlan3"));
                addRoadmapCm.setPcbaCompanyId(Integer.parseInt(pcbaCompanyId));
                addRoadmapCm.setCreateTime(d);
                addRoadmapCm.setVersionY(Integer.parseInt(versionY));
                addRoadmapCm.setVersionN(versionN);
                roadmapCmDao.addRoadmapCm(addRoadmapCm);
                list.add(addRoadmapCm);
            }

            // 操作日志
            operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, d, null, JSONObject.toJSONString(list)));

            // 判断 是否存在认证 数据，有的话update，没有的话insert
            RoadmapCmStatus roadmapCmStatus = roadmapCmStatusDao.getRoadmapCmStatusByCompanyAndVersion(Integer.parseInt(pcbaCompanyId), Integer.parseInt(versionY), versionN);
            if (roadmapCmStatus != null) {
                // update
                RoadmapCmStatus updateRoadmapCmStatus = new RoadmapCmStatus();
                updateRoadmapCmStatus.setUpdateTime(d);
                updateRoadmapCmStatus.setStatus(4);
                updateRoadmapCmStatus.setRoadmapCmStatusId(roadmapCmStatus.getRoadmapCmStatusId());
                roadmapCmStatusDao.updateRoadmapCmStatus(updateRoadmapCmStatus);

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, d, JSONObject.toJSONString(roadmapCmStatus), JSONObject.toJSONString(updateRoadmapCmStatus)));
            } else {
                // insert
                RoadmapCmStatus addRoadmapCmStatus = new RoadmapCmStatus();
                addRoadmapCmStatus.setCreateTime(d);
                addRoadmapCmStatus.setStatus(4);
                addRoadmapCmStatus.setPcbaCompanyId(Integer.parseInt(pcbaCompanyId));
                addRoadmapCmStatus.setVersionN(versionN);
                addRoadmapCmStatus.setVersionY(Integer.parseInt(versionY));
                roadmapCmStatusDao.addRoadmapCmStatus(addRoadmapCmStatus);

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, d, null, JSONObject.toJSONString(addRoadmapCmStatus)));
            }

            return ResultUtil.success(null);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result exportExcelOfCMRoadmaps(String pcbaCompanyIdsStr, Integer year, Integer versionY, String versionN) {
        try {
            if (pcbaCompanyIdsStr == null || "".equals(pcbaCompanyIdsStr)) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyIdsStr", "Miss Param pcbaCompanyIdsStr", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param versionN", null);
            }
            // 查询roadmap 所有大类
            List<RoadmapBigType> allRoadmapBigType = roadmapBigTypeDao.getAllRoadmapBigType(null);
            for (RoadmapBigType roadmapBigType : allRoadmapBigType) {
                Result res = roadmapSmallTypeService.getCmRoadmapByCompanyIdsAndBigTypeAndCalcCapacityIsMatch2(roadmapBigType.getRoadmapBigTypeId(), pcbaCompanyIdsStr, year, versionY, versionN);
                if (res.getCode() == 200) {
                    List<List<RoadmapSmallType>> finalList = (List<List<RoadmapSmallType>>) res.getData();
                    roadmapBigType.setRoadmapSmallTypeListList(finalList);
                }
            }

            pcbaCompanyDao.getPcbaCompanyById(Integer.parseInt(pcbaCompanyIdsStr));

            String excelName = "CM厂roadmap能力对比表";
            // 创建工作簿对象
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表对象
            Sheet sheet = workbook.createSheet(CommonUtils.handleSheetName(excelName));
            // 创建行和单元格对象
            Row row = sheet.createRow(0);

            CellStyle bigTitleContextCellStyle = workbook.createCellStyle();
            Font bigTitleFont = workbook.createFont();
            bigTitleFont.setBold(true);
            bigTitleFont.setFontHeightInPoints((short) 20);
            bigTitleContextCellStyle.setFont(bigTitleFont);
            bigTitleContextCellStyle.setAlignment(HorizontalAlignment.CENTER);
            bigTitleContextCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            CellStyle bodyContextCellStyle = workbook.createCellStyle();
            DataFormat format = workbook.createDataFormat();
            bodyContextCellStyle.setDataFormat(format.getFormat("@"));
            bodyContextCellStyle.setBorderBottom(BorderStyle.THIN);
            bodyContextCellStyle.setBorderLeft(BorderStyle.THIN);
            bodyContextCellStyle.setBorderRight(BorderStyle.THIN);
            bodyContextCellStyle.setBorderTop(BorderStyle.THIN);
            bodyContextCellStyle.setAlignment(HorizontalAlignment.LEFT);
            bodyContextCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            CellStyle titleContextCellStyle = workbook.createCellStyle();
            titleContextCellStyle.setBorderBottom(BorderStyle.MEDIUM);
            titleContextCellStyle.setBorderLeft(BorderStyle.MEDIUM);
            titleContextCellStyle.setBorderRight(BorderStyle.MEDIUM);
            titleContextCellStyle.setBorderTop(BorderStyle.MEDIUM);
            titleContextCellStyle.setAlignment(HorizontalAlignment.CENTER);
            titleContextCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 设置背景色为红色
            titleContextCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            // 创建字体对象
            Font titleFont = workbook.createFont();
            // 设置字体颜色为蓝色
            titleFont.setColor(IndexedColors.WHITE.getIndex());
            titleFont.setBold(true);
            titleContextCellStyle.setFont(titleFont);
            // 设置填充类型为填充实体背景
            titleContextCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            CellStyle isMatchYesContextCellStyle = workbook.createCellStyle();
            Font isMatchYesTitleFont = workbook.createFont();
            isMatchYesTitleFont.setColor(IndexedColors.GREEN.getIndex());
            isMatchYesContextCellStyle.setFont(isMatchYesTitleFont);
            //isMatchNoContextCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
            //isMatchNoContextCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            CellStyle isMatchNoContextCellStyle = workbook.createCellStyle();
            Font isMatchNoTitleFont = workbook.createFont();
            isMatchNoTitleFont.setColor(IndexedColors.RED.getIndex());
            isMatchNoContextCellStyle.setFont(isMatchNoTitleFont);
            //isMatchNoContextCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
            //isMatchNoContextCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            String[] companyArr = pcbaCompanyIdsStr.split(",");
            List<String> companyNameList = new ArrayList<>();
            for (String s : companyArr) {
                PcbaCompany pcbaCompany = pcbaCompanyDao.getPcbaCompanyById(Integer.parseInt(s));
                companyNameList.add(pcbaCompany.getPcbaCompanyNameCn());
            }

            // 第一行大标题
            Cell cell = row.createCell(0);
            cell.setCellValue("大类");
            sheet.setColumnWidth(0, 7000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(1);
            cell.setCellValue("小类");
            sheet.setColumnWidth(1, 7000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(2);
            cell.setCellValue("能力项");
            sheet.setColumnWidth(2, 14000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(3);
            cell.setCellValue("key值");
            sheet.setColumnWidth(3, 3000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(4);
            cell.setCellValue("说明");
            sheet.setColumnWidth(4, 14000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(5);
            cell.setCellValue("数据格式");
            sheet.setColumnWidth(5, 10000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(6);
            cell.setCellValue("H3C能力roadmap");
            cell.setCellStyle(titleContextCellStyle);

            // 序号为 10 开始 每个工厂 占 4 列
            for (int e = 0; e < companyNameList.size(); e++) {
                cell = row.createCell(9 + e * 4 + 1);
                cell.setCellValue(companyNameList.get(e) + " 厂能力值");
                cell.setCellStyle(titleContextCellStyle);
            }

            // 第二行
            row = sheet.createRow(1);

            //补充第一行【不然边框渲染不完整】
            cell = row.createCell(0);
            cell.setCellValue("大类");
            sheet.setColumnWidth(0, 7000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(1);
            cell.setCellValue("小类");
            sheet.setColumnWidth(1, 7000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(2);
            cell.setCellValue("能力项");
            sheet.setColumnWidth(2, 14000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(3);
            cell.setCellValue("key值");
            sheet.setColumnWidth(3, 3000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(4);
            cell.setCellValue("说明");
            sheet.setColumnWidth(4, 14000);
            cell.setCellStyle(titleContextCellStyle);

            cell = row.createCell(5);
            cell.setCellValue("数据格式");
            sheet.setColumnWidth(5, 10000);
            cell.setCellStyle(titleContextCellStyle);

            // 开始第二行标头
            cell = row.createCell(6);
            cell.setCellValue("");
            sheet.setColumnWidth(6, 5000);
            cell.setCellStyle(titleContextCellStyle);
            cell = row.createCell(7);
            cell.setCellValue(year + "年");
            sheet.setColumnWidth(7, 5000);
            cell.setCellStyle(titleContextCellStyle);
            cell = row.createCell(8);
            cell.setCellValue((year + 1) + "年");
            sheet.setColumnWidth(8, 5000);
            cell.setCellStyle(titleContextCellStyle);
            cell = row.createCell(9);
            cell.setCellValue((year + 2) + "年");
            sheet.setColumnWidth(9, 5000);
            cell.setCellStyle(titleContextCellStyle);

            for (int e = 0; e < companyNameList.size(); e++) {
                cell = row.createCell(9 + e * 4 + 1);
                cell.setCellValue("当前能力值");
                cell.setCellStyle(titleContextCellStyle);
                sheet.setColumnWidth(9 + e * 4 + 1, 5000);
                cell = row.createCell(9 + e * 4 + 2);
                cell.setCellValue("能力是否匹配(与" + year + "年相比)");
                cell.setCellStyle(titleContextCellStyle);
                sheet.setColumnWidth(9 + e * 4 + 2, 10000);
                cell = row.createCell(9 + e * 4 + 3);
                cell.setCellValue("能力不匹配原因");
                cell.setCellStyle(titleContextCellStyle);
                sheet.setColumnWidth(9 + e * 4 + 3, 15000);
                cell = row.createCell(9 + e * 4 + 4);
                cell.setCellValue("能力提升计划(如有)");
                cell.setCellStyle(titleContextCellStyle);
                sheet.setColumnWidth(9 + e * 4 + 4, 15000);
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 9 + e * 4 + 1, 9 + e * 4 + 4));
            }

            // 第一行合并单元格
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 6, 9));

            // H3C数据从第三行开始、第0列到8列;
            // CM数据从第三行开始、第9列往后，每个 CM厂占 4列
            if (allRoadmapBigType.size() > 0) {
                int beginRowIndex = 2;
                int index1 = beginRowIndex;
                int index2 = beginRowIndex;
                for (int a = 0; a < allRoadmapBigType.size(); a++) {
                    if (allRoadmapBigType.get(a).getRoadmapSmallTypeListList().size() > 0) {
                        int sizeNum = allRoadmapBigType.get(a).getRoadmapSmallTypeListList().size();
                        List<RoadmapSmallType> roadmapSmallTypeList = allRoadmapBigType.get(a).getRoadmapSmallTypeListList().get(0);
                        for (int b = 0; b < roadmapSmallTypeList.size(); b++) {
                            for (int c = 0; c < roadmapSmallTypeList.get(b).getRoadmapParamList().size(); c++) {
                                // 合并 能力项 一列
                                sheet.addMergedRegion(new CellRangeAddress(beginRowIndex, beginRowIndex + 2, 2, 2));
                                for (int dd = 0; dd < 3; dd++) {
                                    row = sheet.createRow(beginRowIndex);
                                    // 大类
                                    cell = row.createCell(0);
                                    cell.setCellValue(allRoadmapBigType.get(a).getRoadmapBigTypeName());
                                    // 小类
                                    cell = row.createCell(1);
                                    cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapSmallTypeName());
                                    // 能力项
                                    cell = row.createCell(2);
                                    cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamName());
                                    // key值
                                    cell = row.createCell(3);
                                    cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamKey());
                                    // 说明
                                    cell = row.createCell(4);
                                    cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamNotes());
                                    // 数据格式
                                    cell = row.createCell(5);
//                                    1：普通文本
//                                    2：下拉单选
//                                    3：下拉多选
//                                    4：整数
//                                    5：小数
//                                    6：百分比
                                    Integer roadmapParamType = roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamType();
                                    String roadmapParamTypeStr = "";
                                    if (roadmapParamType == 1) {
                                        roadmapParamTypeStr = "普通文本";
                                    } else if (roadmapParamType == 2) {
                                        String roadmapParamOptions = roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamOptions();
                                        roadmapParamTypeStr = "下拉单选(" + roadmapParamOptions + ")";
                                    } else if (roadmapParamType == 3) {
                                        String roadmapParamOptions = roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getRoadmapParamOptions();
                                        roadmapParamTypeStr = "下拉多选(" + roadmapParamOptions + ")";
                                    } else if (roadmapParamType == 4) {
                                        roadmapParamTypeStr = "整数";
                                    } else if (roadmapParamType == 5) {
                                        roadmapParamTypeStr = "小数";
                                    } else if (roadmapParamType == 6) {
                                        roadmapParamTypeStr = "百分比";
                                    }
                                    cell.setCellValue(roadmapParamTypeStr);

                                    if ((beginRowIndex - 2) % 3 == 0) {
                                        cell = row.createCell(6);
                                        cell.setCellValue("量产能力");
                                        cell = row.createCell(7);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C1());
                                        cell = row.createCell(8);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C1Add1Year());
                                        cell = row.createCell(9);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C1Add2Year());
                                    } else if ((beginRowIndex - 2) % 3 == 1) {
                                        cell = row.createCell(6);
                                        cell.setCellValue("小批量能力");
                                        cell = row.createCell(7);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C2());
                                        cell = row.createCell(8);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C2Add1Year());
                                        cell = row.createCell(9);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C2Add2Year());
                                    } else {
                                        cell = row.createCell(6);
                                        cell.setCellValue("预研能力");
                                        cell = row.createCell(7);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C3());
                                        cell = row.createCell(8);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C3Add1Year());
                                        cell = row.createCell(9);
                                        cell.setCellValue(roadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValueH3C3Add2Year());
                                    }

                                    for (int e = 0; e < sizeNum; e++) {
                                        List<RoadmapSmallType> newRoadmapSmallTypeList = allRoadmapBigType.get(a).getRoadmapSmallTypeListList().get(e);

                                        if ((beginRowIndex - 2) % 3 == 0) {
                                            cell = row.createCell(9 + e * 4 + 1);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValue1());
                                            cell = row.createCell(9 + e * 4 + 2);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch1());
                                            if ("是".equals(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch1())) {
                                                cell.setCellStyle(isMatchYesContextCellStyle);
                                            } else {
                                                cell.setCellStyle(isMatchNoContextCellStyle);
                                            }
                                            cell = row.createCell(9 + e * 4 + 3);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getCauses1());
                                            cell = row.createCell(9 + e * 4 + 4);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getPlan1());
                                        } else if ((beginRowIndex - 2) % 3 == 1) {
                                            cell = row.createCell(9 + e * 4 + 1);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValue2());
                                            cell = row.createCell(9 + e * 4 + 2);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch2());
                                            if ("是".equals(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch2())) {
                                                cell.setCellStyle(isMatchYesContextCellStyle);
                                            } else {
                                                cell.setCellStyle(isMatchNoContextCellStyle);
                                            }
                                            cell = row.createCell(9 + e * 4 + 3);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getCauses2());
                                            cell = row.createCell(9 + e * 4 + 4);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getPlan2());
                                        } else {
                                            cell = row.createCell(9 + e * 4 + 1);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getValue3());
                                            cell = row.createCell(9 + e * 4 + 2);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch3());
                                            if ("是".equals(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getIsMatch3())) {
                                                cell.setCellStyle(isMatchYesContextCellStyle);
                                            } else {
                                                cell.setCellStyle(isMatchNoContextCellStyle);
                                            }
                                            cell = row.createCell(9 + e * 4 + 3);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getCauses3());
                                            cell = row.createCell(9 + e * 4 + 4);
                                            cell.setCellValue(newRoadmapSmallTypeList.get(b).getRoadmapParamList().get(c).getPlan3());
                                        }
                                    }
                                    beginRowIndex++;
                                }
                            }
                            // 合并 小类 一列
                            if (index2 <= beginRowIndex - 1) {
                                sheet.addMergedRegion(new CellRangeAddress(index2, beginRowIndex - 1, 1, 1));
                            }
                            index2 = beginRowIndex;
                        }
                        // 合并 大类 一列
                        if (index1 <= beginRowIndex - 1) {
                            sheet.addMergedRegion(new CellRangeAddress(index1, beginRowIndex - 1, 0, 0));
                        }
                        index1 = beginRowIndex;
                    }
                }
            }

            String fileName = excelName + CommonUtils.getTimestamp() + ".xlsx";
            String pathPrefix = fileSaveUrl + "exportExcels\\";
            File folder = new File(pathPrefix);
            if (!folder.isDirectory()) {
                folder.mkdirs();
            }
            String filePath = pathPrefix + fileName;
            // 导出Excel文件
            FileOutputStream outputStream = new FileOutputStream(filePath);
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();

            byte[] data = null;
            InputStream in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
            java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
            String base64 = encoder.encodeToString(data);

            Map<String, Object> resMap = new HashMap<>();
            resMap.put("base64", base64);
            resMap.put("fileName", fileName);
            resMap.put("allRoadmapBigType", allRoadmapBigType);
            return ResultUtil.success(resMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager1")
    @Override
    public Result importRoadmapCmExcel(MultipartFile file, Integer pcbaCompanyId) {
        try {
            if (file == null) {
                return ResultUtil.error(500, "缺少参数file", "Miss Param file", null);
            }
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            List<Map<String, Object>> list = new ArrayList<>();
            // 1.得到一个输入流对象
            //FileInputStream in = FileConverterUtils.convertTo(file);
            InputStream inputStream = file.getInputStream();
            File f = FileConverterUtils.getTempFileFromInputStream(inputStream);
            // 2.将输入流对象进行解析 得到workbook对象
            //Workbook workbook = new XSSFWorkbook(in);
            Workbook workbook = WorkbookFactory.create(f);
            // 3.workbook中有几个工作簿
            int sheetNum = workbook.getNumberOfSheets();
            System.out.println("一共有" + sheetNum + "个工作簿");
            // 3.1通过workbook 创建工作簿对象
            //Sheet sheet1 = workbook.getSheetAt(0);
            //Sheet sheet2 = workbook.getSheetAt(1);

            // 查询 roadmap最新版本
            Integer version_y = 0;
            String version_n = "";
            List<RoadmapH3c> historyVersion = roadmapH3cDao.getHistoryVersion();
            if (historyVersion.size() > 0) {
                version_y = historyVersion.get(historyVersion.size() - 1).getVersionY();
                version_n = historyVersion.get(historyVersion.size() - 1).getVersionN();
            }
            if (version_y == 0 || "".equals(version_n)) {
                return ResultUtil.error(500, "未能获取到最新版本", "Not Found Newest Version", null);
            }

            // 今年
            Calendar cal = Calendar.getInstance();
            Integer year = cal.get(Calendar.YEAR);
            //明年
            Integer yearAdd1 = year + 1;
            //后年
            Integer yearAdd2 = year + 2;

            for (int a = 0; a < 1; a++) {
                Sheet sheet = workbook.getSheetAt(a);
                int allRows = sheet.getLastRowNum() + 1;
                System.out.println("sheet" + (a + 1) + "一共有：" + allRows + "行");
                String sheetName = sheet.getSheetName();
                System.out.println("当前sheetName：" + sheetName);

                Map<String, Object> map = new HashMap<>();
                //标题行 第1行开始
                Row row = sheet.getRow(0);
                String t1 = row.getCell(0) != null ? row.getCell(0).toString() : "";
                String t2 = row.getCell(1) != null ? row.getCell(1).toString() : "";
                String t3 = row.getCell(2) != null ? row.getCell(2).toString() : "";
                String t4 = row.getCell(3) != null ? row.getCell(3).toString() : "";
                String t5 = row.getCell(4) != null ? row.getCell(4).toString() : "";
                String t6 = row.getCell(5) != null ? row.getCell(5).toString() : "";
                String t7 = row.getCell(6) != null ? row.getCell(6).toString() : "";

                //第二行标题
                row = sheet.getRow(1);
                String t11 = row.getCell(10) != null ? row.getCell(10).toString() : "";
                String t13 = row.getCell(12) != null ? row.getCell(12).toString() : "";
                String t14 = row.getCell(13) != null ? row.getCell(13).toString() : "";
                if (t1.contains("大类") &&
                        t2.contains("小类") &&
                        t3.contains("能力项") &&
                        t4.contains("key值") &&
                        t5.contains("说明") &&
                        t6.contains("数据格式") &&
                        t7.contains("H3C能力roadmap") &&
                        t11.contains("当前能力值") &&
                        t13.contains("能力不匹配原因") &&
                        t14.contains("能力提升计划")
                ) {
                    // 表头符合的，循环获取每行数据
                    //List<Map<String,Object>> list2 = new ArrayList<>();
                    for (int b = 2; b < allRows; b++) {
                        Row rowV = sheet.getRow(b);
                        String v4 = rowV.getCell(3) != null ? rowV.getCell(3).toString() : "";
                        String v11 = rowV.getCell(10) != null ? rowV.getCell(10).toString() : "";
                        String v13 = rowV.getCell(12) != null ? rowV.getCell(12).toString() : "";
                        String v14 = rowV.getCell(13) != null ? rowV.getCell(13).toString() : "";
                        // 只要管 第4列、 第11列、第13列、第14列的值即可，为 当前能力值、能力不匹配原因、能力提升计划

                        Map<String, Object> map2 = new HashMap<>();
                        // 从第 3 行开始，每行取值
                        if ((b - 2) % 3 == 0) {
                            map2.put("roadmapCmParamKey", v4);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("roadmapCmParamValue", v11);
                            map3.put("abilityMismatchCauses", v13);
                            map3.put("abilityImprovePlan", v14);
                            map2.put("roadmapCmParamValue1", map3);
                        }
                        if ((b - 2) % 3 == 1) {
                            map2.put("roadmapCmParamKey", v4);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("roadmapCmParamValue", v11);
                            map3.put("abilityMismatchCauses", v13);
                            map3.put("abilityImprovePlan", v14);
                            map2.put("roadmapCmParamValue2", map3);
                        }
                        if ((b - 2) % 3 == 2) {
                            map2.put("roadmapCmParamKey", v4);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("roadmapCmParamValue", v11);
                            map3.put("abilityMismatchCauses", v13);
                            map3.put("abilityImprovePlan", v14);
                            map2.put("roadmapCmParamValue3", map3);
                        }
                        list.add(map2);
                    }
                } else {
                    return ResultUtil.error(500, "模板错误", "Error Template", null);
                }
            }

            // 重新构造 list
            List<Map<String, Object>> finalList = new ArrayList<>();
            List<String> keyList = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : list) {
                String roadmapCmParamKey = (String) stringObjectMap.get("roadmapCmParamKey");
                if (!keyList.contains(roadmapCmParamKey)) {
                    keyList.add(roadmapCmParamKey);
                }
            }
            for (String key : keyList) {
                String roadmapCmParamValue1 = "";// 量产能力值 当前值
                String roadmapCmParamValue2 = "";// 小批量能力 当前值
                String roadmapCmParamValue3 = "";// 预研能力 当前值
                String abilityMismatchCauses1 = "";// 量产能力 能力不匹配原因
                String abilityMismatchCauses2 = "";// 小批量能力 能力不匹配原因
                String abilityMismatchCauses3 = "";// 预研能力 能力不匹配原因
                String abilityImprovePlan1 = "";// 量产能力 能力提升计划
                String abilityImprovePlan2 = "";// 小批量能力 能力提升计划
                String abilityImprovePlan3 = "";// 预研能力 能力提升计划
                for (Map<String, Object> stringObjectMap : list) {
                    String roadmapH3cParamKey = (String) stringObjectMap.get("roadmapCmParamKey");
                    if (key.equals(roadmapH3cParamKey) && stringObjectMap.get("roadmapCmParamValue1") != null) {
                        Map<String, Object> roadmapCmParamValueMap = (Map<String, Object>) stringObjectMap.get("roadmapCmParamValue1");
                        String roadmapCmParamValue = (String) roadmapCmParamValueMap.get("roadmapCmParamValue");
                        String abilityMismatchCauses = (String) roadmapCmParamValueMap.get("abilityMismatchCauses");
                        String abilityImprovePlan = (String) roadmapCmParamValueMap.get("abilityImprovePlan");
                        roadmapCmParamValue1 = roadmapCmParamValue;
                        abilityMismatchCauses1 = abilityMismatchCauses;
                        abilityImprovePlan1 = abilityImprovePlan;
                        break;
                    }
                }
                for (Map<String, Object> stringObjectMap : list) {
                    String roadmapH3cParamKey = (String) stringObjectMap.get("roadmapCmParamKey");
                    if (key.equals(roadmapH3cParamKey) && stringObjectMap.get("roadmapCmParamValue2") != null) {
                        Map<String, Object> roadmapCmParamValueMap = (Map<String, Object>) stringObjectMap.get("roadmapCmParamValue2");
                        String roadmapCmParamValue = (String) roadmapCmParamValueMap.get("roadmapCmParamValue");
                        String abilityMismatchCauses = (String) roadmapCmParamValueMap.get("abilityMismatchCauses");
                        String abilityImprovePlan = (String) roadmapCmParamValueMap.get("abilityImprovePlan");
                        roadmapCmParamValue2 = roadmapCmParamValue;
                        abilityMismatchCauses2 = abilityMismatchCauses;
                        abilityImprovePlan2 = abilityImprovePlan;
                        break;
                    }
                }
                for (Map<String, Object> stringObjectMap : list) {
                    String roadmapH3cParamKey = (String) stringObjectMap.get("roadmapCmParamKey");
                    if (key.equals(roadmapH3cParamKey) && stringObjectMap.get("roadmapCmParamValue3") != null) {
                        Map<String, Object> roadmapCmParamValueMap = (Map<String, Object>) stringObjectMap.get("roadmapCmParamValue3");
                        String roadmapCmParamValue = (String) roadmapCmParamValueMap.get("roadmapCmParamValue");
                        String abilityMismatchCauses = (String) roadmapCmParamValueMap.get("abilityMismatchCauses");
                        String abilityImprovePlan = (String) roadmapCmParamValueMap.get("abilityImprovePlan");
                        roadmapCmParamValue3 = roadmapCmParamValue;
                        abilityMismatchCauses3 = abilityMismatchCauses;
                        abilityImprovePlan3 = abilityImprovePlan;
                        break;
                    }
                }

                JSONObject jo = new JSONObject();
                jo.put("roadmapCmParamKey", key);
                JSONObject jo1 = new JSONObject();
                jo1.put("roadmapCmParamValue1", roadmapCmParamValue1 != null ? CommonUtils.removeZeros(roadmapCmParamValue1) : "");
                jo1.put("abilityMismatchCauses1", abilityMismatchCauses1 != null ? CommonUtils.removeZeros(abilityMismatchCauses1) : "");
                jo1.put("abilityImprovePlan1", abilityImprovePlan1 != null ? CommonUtils.removeZeros(abilityImprovePlan1) : "");
                jo.put("roadmapCmParamValue1", jo1);

                JSONObject jo2 = new JSONObject();
                jo2.put("roadmapCmParamValue2", roadmapCmParamValue2 != null ? CommonUtils.removeZeros(roadmapCmParamValue2) : "");
                jo2.put("abilityMismatchCauses2", abilityMismatchCauses2 != null ? CommonUtils.removeZeros(abilityMismatchCauses2) : "");
                jo2.put("abilityImprovePlan2", abilityImprovePlan2 != null ? CommonUtils.removeZeros(abilityImprovePlan2) : "");
                jo.put("roadmapCmParamValue2", jo2);

                JSONObject jo3 = new JSONObject();
                jo3.put("roadmapCmParamValue3", roadmapCmParamValue3 != null ? CommonUtils.removeZeros(roadmapCmParamValue3) : "");
                jo3.put("abilityMismatchCauses3", abilityMismatchCauses3 != null ? CommonUtils.removeZeros(abilityMismatchCauses3) : "");
                jo3.put("abilityImprovePlan3", abilityImprovePlan3 != null ? CommonUtils.removeZeros(abilityImprovePlan3) : "");
                jo.put("roadmapCmParamValue3", jo3);
                finalList.add(jo);
            }

            Date d = new Date();
            String userId = UserHeaderHolder.getUserId();

            List<RoadmapCm> oldList = new ArrayList<>();
            List<RoadmapCm> newList = new ArrayList<>();

            for (Map<String, Object> stringObjectMap : finalList) {
                String roadmapCmParamKey = (String) stringObjectMap.get("roadmapCmParamKey");
                JSONObject jsonObject1 = (JSONObject) stringObjectMap.get("roadmapCmParamValue1");
                String roadmapCmParamValue1 = jsonObject1.getString("roadmapCmParamValue1");
                String abilityMismatchCauses1 = jsonObject1.getString("abilityMismatchCauses1");
                String abilityImprovePlan1 = jsonObject1.getString("abilityImprovePlan1");
                JSONObject jsonObject2 = (JSONObject) stringObjectMap.get("roadmapCmParamValue2");
                String roadmapCmParamValue2 = jsonObject2.getString("roadmapCmParamValue2");
                String abilityMismatchCauses2 = jsonObject2.getString("abilityMismatchCauses2");
                String abilityImprovePlan2 = jsonObject2.getString("abilityImprovePlan2");
                JSONObject jsonObject3 = (JSONObject) stringObjectMap.get("roadmapCmParamValue3");
                String roadmapCmParamValue3 = jsonObject3.getString("roadmapCmParamValue3");
                String abilityMismatchCauses3 = jsonObject3.getString("abilityMismatchCauses3");
                String abilityImprovePlan3 = jsonObject3.getString("abilityImprovePlan3");

                RoadmapCm roadmapCmByKey = roadmapCmDao.getRoadmapCmByCompanyAndKey(pcbaCompanyId, roadmapCmParamKey, version_y, version_n);
                if (roadmapCmByKey == null) {
                    // insert
                    RoadmapCm addRoadmapCm = new RoadmapCm();
                    addRoadmapCm.setRoadmapCmParamKey(roadmapCmParamKey);
                    //roadmapCmParamValue1 是计算的不用管用户的值
                    // 新增判断， 若 valueAlgorithmType 为空，则取自用户填写的值，否则是计算的不管用户填写的值
                    RoadmapParam rk = roadmapParamDao.getRoadmapParamByKey(roadmapCmParamKey);
                    if(rk != null && rk.getRuleAlgorithmType() != null && !"".equals(rk.getRuleAlgorithmType().toString())){
                        // 什么都不用管
                    }else{
                        // 使用用户填写的值
                        addRoadmapCm.setRoadmapCmParamValue1(roadmapCmParamValue1);
                    }
                    addRoadmapCm.setRoadmapCmParamValue2(roadmapCmParamValue2);
                    addRoadmapCm.setRoadmapCmParamValue3(roadmapCmParamValue3);
                    addRoadmapCm.setAbilityMismatchCauses1(abilityMismatchCauses1);
                    addRoadmapCm.setAbilityMismatchCauses2(abilityMismatchCauses2);
                    addRoadmapCm.setAbilityMismatchCauses3(abilityMismatchCauses3);
                    addRoadmapCm.setAbilityImprovePlan1(abilityImprovePlan1);
                    addRoadmapCm.setAbilityImprovePlan2(abilityImprovePlan2);
                    addRoadmapCm.setAbilityImprovePlan3(abilityImprovePlan3);
                    addRoadmapCm.setCreateTime(d);
                    addRoadmapCm.setSort(100);
                    addRoadmapCm.setStatus(1);
                    addRoadmapCm.setPcbaCompanyId(pcbaCompanyId);
                    addRoadmapCm.setVersionY(version_y);
                    addRoadmapCm.setVersionN(version_n);
                    roadmapCmDao.addRoadmapCm(addRoadmapCm);
                    newList.add(addRoadmapCm);
                } else {
                    // update
                    Integer roadmapCmParamId = roadmapCmByKey.getRoadmapCmParamId();
                    String roadmapCmParamValue1_ = roadmapCmByKey.getRoadmapCmParamValue1() != null ? roadmapCmByKey.getRoadmapCmParamValue1() : "";
                    String roadmapCmParamValue2_ = roadmapCmByKey.getRoadmapCmParamValue2() != null ? roadmapCmByKey.getRoadmapCmParamValue2() : "";
                    String roadmapCmParamValue3_ = roadmapCmByKey.getRoadmapCmParamValue3() != null ? roadmapCmByKey.getRoadmapCmParamValue3() : "";
                    String abilityMismatchCauses1_ = roadmapCmByKey.getAbilityMismatchCauses1() != null ? roadmapCmByKey.getAbilityMismatchCauses1() : "";
                    String abilityMismatchCauses2_ = roadmapCmByKey.getAbilityMismatchCauses2() != null ? roadmapCmByKey.getAbilityMismatchCauses2() : "";
                    String abilityMismatchCauses3_ = roadmapCmByKey.getAbilityMismatchCauses3() != null ? roadmapCmByKey.getAbilityMismatchCauses3() : "";
                    String abilityImprovePlan1_ = roadmapCmByKey.getAbilityImprovePlan1() != null ? roadmapCmByKey.getAbilityImprovePlan1() : "";
                    String abilityImprovePlan2_ = roadmapCmByKey.getAbilityImprovePlan2() != null ? roadmapCmByKey.getAbilityImprovePlan2() : "";
                    String abilityImprovePlan3_ = roadmapCmByKey.getAbilityImprovePlan3() != null ? roadmapCmByKey.getAbilityImprovePlan3() : "";
                    if (!roadmapCmParamValue1.equals(roadmapCmParamValue1_) ||
                            !roadmapCmParamValue2.equals(roadmapCmParamValue2_) ||
                            !roadmapCmParamValue3.equals(roadmapCmParamValue3_) ||
                            !abilityMismatchCauses1.equals(abilityMismatchCauses1_) ||
                            !abilityMismatchCauses2.equals(abilityMismatchCauses2_) ||
                            !abilityMismatchCauses3.equals(abilityMismatchCauses3_) ||
                            !abilityImprovePlan1.equals(abilityImprovePlan1_) ||
                            !abilityImprovePlan2.equals(abilityImprovePlan2_) ||
                            !abilityImprovePlan3.equals(abilityImprovePlan3_)
                    ) {
                        oldList.add(roadmapCmByKey);
                        RoadmapCm updateRoadmapCm = new RoadmapCm();
                        updateRoadmapCm.setRoadmapCmParamKey(roadmapCmParamKey);
                        //roadmapCmParamValue1 是计算的不用管用户的值
                        // 新增判断， 若 valueAlgorithmType 为空，则取自用户填写的值，否则是计算的不管用户填写的值
                        RoadmapParam rk = roadmapParamDao.getRoadmapParamByKey(roadmapCmParamKey);
                        if(rk != null && rk.getRuleAlgorithmType() != null && !"".equals(rk.getRuleAlgorithmType().toString())){
                            // 什么都不用管
                        }else{
                            // 使用用户填写的值
                            updateRoadmapCm.setRoadmapCmParamValue1(roadmapCmParamValue1);
                        }
                        updateRoadmapCm.setRoadmapCmParamValue2(roadmapCmParamValue2);
                        updateRoadmapCm.setRoadmapCmParamValue3(roadmapCmParamValue3);
                        updateRoadmapCm.setAbilityMismatchCauses1(abilityMismatchCauses1);
                        updateRoadmapCm.setAbilityMismatchCauses2(abilityMismatchCauses2);
                        updateRoadmapCm.setAbilityMismatchCauses3(abilityMismatchCauses3);
                        updateRoadmapCm.setAbilityImprovePlan1(abilityImprovePlan1);
                        updateRoadmapCm.setAbilityImprovePlan2(abilityImprovePlan2);
                        updateRoadmapCm.setAbilityImprovePlan3(abilityImprovePlan3);
                        updateRoadmapCm.setCreateTime(d);
                        updateRoadmapCm.setSort(100);
                        updateRoadmapCm.setStatus(1);
                        updateRoadmapCm.setPcbaCompanyId(pcbaCompanyId);
                        updateRoadmapCm.setVersionY(version_y);
                        updateRoadmapCm.setVersionN(version_n);
                        updateRoadmapCm.setRoadmapCmParamId(roadmapCmParamId);
                        roadmapCmDao.updateRoadmapCm(updateRoadmapCm);
                        newList.add(updateRoadmapCm);
                    }
                }
            }

            if (newList.size() > 0) {
                // 操作日志
                // 将状态改为 4 待提交审核
                RoadmapCmStatus roadmapCmStatus = roadmapCmStatusDao.getRoadmapCmStatusByCompanyAndVersion(pcbaCompanyId, version_y, version_n);
                if (roadmapCmStatus != null) {
                    // update
                    RoadmapCmStatus updateRoadmapCmStatus = new RoadmapCmStatus();
                    updateRoadmapCmStatus.setUpdateTime(d);
                    updateRoadmapCmStatus.setStatus(4);
                    updateRoadmapCmStatus.setRoadmapCmStatusId(roadmapCmStatus.getRoadmapCmStatusId());
                    roadmapCmStatusDao.updateRoadmapCmStatus(updateRoadmapCmStatus);

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, d, JSONObject.toJSONString(roadmapCmStatus), JSONObject.toJSONString(updateRoadmapCmStatus)));
                } else {
                    // insert
                    RoadmapCmStatus addRoadmapCmStatus = new RoadmapCmStatus();
                    addRoadmapCmStatus.setCreateTime(d);
                    addRoadmapCmStatus.setStatus(4);
                    addRoadmapCmStatus.setPcbaCompanyId(pcbaCompanyId);
                    addRoadmapCmStatus.setVersionN(version_n);
                    addRoadmapCmStatus.setVersionY(version_y);
                    roadmapCmStatusDao.addRoadmapCmStatus(addRoadmapCmStatus);

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, d, null, JSONObject.toJSONString(addRoadmapCmStatus)));
                }

                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, d, JSONObject.toJSONString(oldList), JSONObject.toJSONString(newList)));

            }
            Map<String, Object> m = new HashMap<>();
            m.put("finalList", finalList);
            m.put("newList", newList);
            return ResultUtil.success(m);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
